import request from '@/utils/request'
import { ApiResponse } from '@/types/api'

// 用户组相关接口
export const group = {
  // 获取用户组列表
  getGroupList: (params?: any): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/user/group/list',
      method: 'get',
      params,
    })
  },

  // 查看单个用户组详情
  getGroupDetail: (id: number): Promise<ApiResponse<any>> => {
    return request({
      url: `/api/user/group/${id}`,
      method: 'get',
    })
  },

  // 创建用户组
  addGroup: (data: any): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/user/group',
      method: 'post',
      data,
    })
  },

  // 更新用户组
  updateGroup: (data: any): Promise<ApiResponse<any>> => {
    return request({
      url: `/api/user/group/${data.id}`,
      method: 'put',
      data,
    })
  },

  // 删除用户组
  deleteGroup: (id: number): Promise<ApiResponse<any>> => {
    return request({
      url: `/api/user/group/${id}`,
      method: 'delete',
    })
  },

  // 获取启用的用户组列表（用于下拉选择）
  getActiveGroupList: (): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/user/group/active-list',
      method: 'get',
    })
  },

  // 批量更新用户组状态
  batchUpdateGroupStatus: (data: any): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/user/group/batch-status',
      method: 'post',
      data,
    })
  },
}

// 向后兼容方法
export const getGroupList = group.getGroupList
export const getGroupDetail = group.getGroupDetail
export const saveGroup = (data: any): Promise<ApiResponse<any>> => {
  // 根据是否有id判断是新增还是编辑
  if (data.id && data.id > 0) {
    return group.updateGroup(data)
  } else {
    return group.addGroup(data)
  }
}
export const deleteGroup = group.deleteGroup
export const getActiveGroupList = group.getActiveGroupList
export const batchUpdateGroupStatus = group.batchUpdateGroupStatus
